package com.sy.server.dao;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Repository;

import java.io.Serializable;
import java.util.Set;
import java.util.concurrent.TimeUnit;

@Repository
public class RedisDAO {
    @Autowired
    private RedisTemplate<Serializable,Serializable> redisTemplate;
    @Autowired
    private RedisTemplate<Serializable,Serializable> redisTemplate1;

    public Boolean setNx(String k,Object v,long timeout,TimeUnit timeUnit){
        ValueOperations valueOperations=redisTemplate1.opsForValue();
        return valueOperations.setIfAbsent(k,v,timeout,timeUnit);
    }

    public Object getString(String k) {
        ValueOperations valueOperations=redisTemplate1.opsForValue();
        return valueOperations.get(k);
    }

    public Set keys(String k){
        return redisTemplate1.keys(k);
    }

    public Boolean deleteString(String k){
        return redisTemplate1.delete(k);
    }

    public Boolean getBit(String key,Long offset){
        return redisTemplate1.execute((RedisCallback<Boolean>)  redisConnection -> redisConnection.getBit(key.getBytes(),offset));
    }

    public Boolean setBit(String key,long offset,boolean status){
        return redisTemplate1.execute((RedisCallback<Boolean>)  redisConnection -> redisConnection.setBit(key.getBytes(),offset,status));
    }

    public Long bitCount(String key) {
        return redisTemplate.execute((RedisCallback<Long>) con -> con.bitCount(key.getBytes()));
    }
}
